<?php
namespace App\Admin\Forms;

use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;
use App\Models\Device;
use App\Models\Part;
use Illuminate\Support\Facades\Log;

class ScrapEquipment extends Form implements LazyRenderable
{
    use LazyWidget; // 使用异步加载功能
    protected $title = '报废设备';
    // 处理请求

    public function handle(array $input)
    {
        //获取当前行ID
        $id = $this->payload['id'] ?? null;
        

        // 然后在Part模型中根据当前行ID查找
        $part = Part::query()->find($id);
       
        if ($part) {
            $part->state = '报废';
            $date = date('Y-m-d');
            $assetNumber = $part->asset_number;
            $categoryId = $part->Parts_classification;
            $categoryName = \App\Models\Category::find($categoryId)->category ?? '';
            
            // 保存更新
            $part->save();
            $reason = request()->input('报废原因'); // 获取更换原因字段值
            Log::info("报废配件操作 ---> 日期:[{$date}] -- 类型：[{$categoryName}] -- 资产编号：[{$assetNumber}] -- 报废原因:{$reason}");
            return $this->response()->success('报废设备更新成功')->refresh('Dcat.reload()');
            
        }
       
        // 返回错误的消息
        return $this->response()->error('报废设备更新失败');
    }
    public function form()
    {
        $this->select('state')->options(['报废' => '报废'])->default('报废')->required();
        $this->text('报废原因')->required();
    }
}